package com.tfjybj.itoo.exam.websocket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/demowebsockt")
public class DemoWebsocketController {
    @OnOpen
    public void open(Session session){
//        一个session 代表一个通信会话
        System.out.println("session:"+ session.getId());
    }
    @OnClose
    public  void close(){

    }
    @OnMessage
    public void echomessageandsendmessage(Session session, String mes){
        System.out.println("接收到："+ mes);
        // 发送服务端消息
        try {
            session.getBasicRemote().sendText("来自服务端的消息");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
